﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using COM.PreAlarm.BLL;
using COM.PreAlarm.Model;
using System.Data;
using COM.Rights.BLL;
using COM.Rights.Model;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
///WebServiceForMap 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]
public class WebServiceForMap : System.Web.Services.WebService {

    public WebServiceForMap () {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    /// <summary>
    ///从配置文件中 获取地图服务的ip地址
    /// </summary>
    /// <returns></returns>
    [WebMethod(Description="获取地图的服务地址")]
    public string GetMapIP()
    {
        string strIP = ConfigurationManager.AppSettings["mapIp"];
        return  strIP;
    }

   /// <summary>
   /// 获取信息发送列表
   /// </summary>
   /// <returns></returns>
    [WebMethod(Description = "获取信息发送列表")]
    public List<Msg_SentInfo> GetMsgSentList()
    {
        Msg_Sent ms = new Msg_Sent();
        List<Msg_SentInfo> list = ms.GetList();
        return list;
    }

    /// <summary>
    /// 单个信息发送
    /// </summary>
    [WebMethod(Description = "单个信息发送")]
    public int  SendMessage(string equipNo,string tMsg)
    {
        
        //生成信息编码
        string msgNo = equipNo.Substring(0, 4) + "FS_" + string.Format("{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));
        COM.PreAlarm.Model.Msg_SentInfo msi = new COM.PreAlarm.Model.Msg_SentInfo();
        msi.Msg_No = msgNo;
        msi.Msg_Content = tMsg;
        COM.PreAlarm.BLL.Msg_Sent msg = new COM.PreAlarm.BLL.Msg_Sent();
        msg.Add(msi, "*");//插入信息表

        COM.PreAlarm.BLL.Equip_MsgSent ems = new COM.PreAlarm.BLL.Equip_MsgSent();
        COM.PreAlarm.Model.Equip_MsgSentInfo emsi = new COM.PreAlarm.Model.Equip_MsgSentInfo();
        emsi.SetIsNew(true);
        emsi.Equip_No = equipNo;
        emsi.Msg_No = msi.Msg_No;
        emsi.Msg_SendTime = DateTime.Now;
        emsi.Msg_RecStatus = 0;
        ems.Add(emsi, "*");//插入信息备案表
        return 0;
    }

    /// <summary>
    /// 获取用户权限下
    /// </summary>
    /// <param name="uid"></param>
    /// <returns></returns>
    [WebMethod(Description = "获取用户权限")]
    public List<UserAuth> GetUserAuth(string uid)
    {
        TUser tUser = new TUser();
        List<DBParameter> para = new List<DBParameter>();
        para.Add(new DBParameter("@UID", uid));
        IDataReader sdr = tUser.ExecuteReader("[dbo].[GetUserCityAndUid]", para, System.Data.CommandType.StoredProcedure);
        DataTable dt = new DataTable();
        dt.Load(sdr);
        List<UserAuth> list = new List<UserAuth>();
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(ToModel(dr));
            }
        }

        return list;
    }

    
    /// <summary>
    /// //获取某个权限下的数据条数
    /// </summary>
    /// <param name="name">传入城市名称 </param>
    /// <param name="nameLength">用户权限对用的代码长度</param>
    /// <returns></returns>
    [WebMethod(Description = "获取某个权限下的数据条数")]
    public List<VillageInfo> GetOnlineStatus(string name, string value)
    {
        int nameLength = value.Length;
        string online = OnlineXX();
        TUser tUser = new TUser();
        List<DBParameter> para = new List<DBParameter>();
        para.Add(new DBParameter("@Online", online));
        para.Add(new DBParameter("@Name", name));
        para.Add(new DBParameter("@NameLength", nameLength));
        IDataReader sdr = tUser.ExecuteReader("[dbo].[GetEquipInstalledOnlineStatus]", para, System.Data.CommandType.StoredProcedure);
        DataTable dt = new DataTable();
        dt.Load(sdr);
        List<VillageInfo> list = new List<VillageInfo>();
        if(dt.Rows.Count>0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(ToModelV(dr));
            }
        }
        return list;
    }

    //获取当前时间的字符串
    private string OnlineXX()
    {
        string online = "Online_";
        DateTime dt = DateTime.Now;
        int hh = dt.Hour;
        if (hh < 10)
        {
            online += "0" + hh;
        }
        else
        {
            online += hh;
        }
        return online;
    }


    /// <summary>
    /// 将一行权限值数据转化为一个对象
    /// </summary>
    /// <param name="reader"></param>
    /// <returns></returns>
    public UserAuth ToModel(DataRow dr)
    {
        UserAuth ua = new UserAuth();

        ua.Name = (string)dr["Name"].ToString();
        ua.OrganId = (string)dr["OrganID"].ToString();
        return ua;
    }

    /// <summary>
    /// 将一行权限值数据转化为一个对象
    /// </summary>
    /// <param name="reader"></param>
    /// <returns></returns>
    public VillageInfo ToModelV(DataRow dr)
    {
        VillageInfo vi= new VillageInfo();
        vi.ENo = dr["Equip_No"].ToString();
        vi.ESimNo = dr["Equip_SimNo"].ToString();
        vi.EVillage = dr["Equip_Village"].ToString();
        vi.ELng = Convert.ToDecimal(dr["Equip_Longitude"]);
        vi.ELat = Convert.ToDecimal(dr["Equip_Latitude"]);
        vi.EOnline = Convert.ToByte(dr["Online"]);
        vi.EClass = Convert.ToByte(dr["Equip_Class"]);
        return vi;
    }
}
    /// <summary>
    /// 用户权限类 Name:用户权限下的城市或县城名，OrganId：用户权限码长度
    /// </summary>
    public class UserAuth
    {
        public string Name
        {
            set;
            get;
        }

        public string OrganId
        {
            set;
            get;
        }
    }


    /// <summary>
    /// 村庄在线信息对象
    /// </summary>
    public class VillageInfo
    {
        public string ENo
        {
            set;
            get;
        }

        public string ESimNo
        {
            set;
            get;
        }

        public string EVillage
        {
            set;
            get;
        }

        public Decimal ELng
        {
            set;
            get;
        }

        public Decimal ELat
        {
            set;
            get;
        }

        public Byte EOnline
        {
            set;
            get;
        }

        public Byte EClass
        {
            set;
            get;
        }
    }
    

